package com.hc.model.code04.test7_1;

import java.util.ArrayList;
import java.util.List;

/**
 * @version v1.0
 * @ClassName: ChatMediatorImpl
 * @Description: 中介者的具体实现
 * @Author: HC
 */
public class ChatMediatorImpl extends ChatMediator{

    // 创建一个容器存储同事对象
    private List<User> users;

    public ChatMediatorImpl() {
        this.users = new ArrayList<>();
    }

    @Override
    void sendMessage(String message, User user) {
        // 将消息发给除自己以外的同事
        for (User u : this.users) {
            if (u != user) {
                // 其他用户接收消息
                u.receiveMessage(message);
            }
        }
    }

    @Override
    void addUser(User user) {
        users.add(user);
    }
}
